import Config from "../config/Config"
import Snake, { ISnake } from "./Snake"
import SnakeNode from "./SnakeNode"
import { RandUtil } from "../util/RandUtil"
import { Util } from "../util/Util"
import AITurn from "./ai/AITurn"
import BaseAI from "./ai/BaseAI"
import AIEat from "./ai/AIEat"
import AIAwayWall from "./ai/AIAwayWall"
import AIAwaySnake from "./ai/AIAwaySnake"
import AIFindSnake from "./ai/AIFindSnake"

const util = Util
const cfg = Config.AISnake
const ru = RandUtil

export interface IAIVal {
    nearestNode?: SnakeNode,
}

export default class AISnake extends Snake {

    aiLevel: number;

    private _ais: BaseAI[] = []
    private _aiVal: IAIVal = {}

    get timer() { return this._timer }
    get aiVal() { return this._aiVal }

    /** 实例化电脑蛇 */
    constructor(obj: ISnake) {
        super(obj);
        if (!obj.name) {
            //生成AI名
            let name = '';
            do name = cfg.namePre + util.zeroFill(ru.rand(0, 1000, true), 3)
            while (this.world.getSnake(name))
            this._name = name;
        }
    }

    spawn() {
        // this.aiLevel = ru.chance(0.33) ? ru.chance(0.33) ? 5 : ru.rand(2, 5, true) : ru.rand(1, 3, true)
        this.aiLevel = 1

        this._ais = []
        switch (this.aiLevel) {
            case 1:
                this._ais.push(
                    new AITurn({
                        snake: this,
                        checkTime: [233, 1333],
                        aiChance: [0.233, 0.888],
                        aiTime: [100, 888],
                        turnSpd: [0.1, 1],
                    }),
                    new AIEat({
                        snake: this,
                        checkTime: [233, 1333],
                        aiChance: [0.233, 0.888],
                        aiTime: [233, 2333],
                        findDis: [2.33, 8.88]
                    }),
                    new AIAwayWall({
                        snake: this,
                        checkTime: [123, 666],
                        aiChance: [0.666, 0.888],
                        checkDis: [8.88, 16.66],
                        aiTime: [888, 2333],
                        offsetAng: [0, 45]
                    }),
                    new AIAwaySnake({
                        snake: this,
                        checkTime: [123, 256],
                        aiChance: [0.666, 0.888],
                        aiTime: [888, 2333],
                        checkDis: [8.88, 16.66],
                        offsetAng: [0, 88]
                    }),
                    new AIFindSnake({
                        snake: this,
                        checkTime: [512, 1024],
                        aiChance: [0.333, 0.666],
                        aiTime: [233, 1233],
                        findDis: [5, 20],
                        stopDis: [5, 10],
                    })
                )
                break
        }
        this._ais.sort((a, b) => b.priority - a.priority)
        super.spawn();
    }

    ai() {
        this.aiIdle();
        this._ais.forEach(ai => {
            ai.check()
        })
    }

    /** 清除状态 */
    aiIdle() {
        this._head.toAng = this._head.ang;
        this._head.isSprint = false;
    }


    updateAfterCheck() {
        if (!this._isDeath) {
            this.ai()
        }
        super.updateAfterCheck()
    }
}